local wifista = {
    onfunc_ = function() print("old") end ,
    failfunc_ = function() end 
}
--==================================
--default params
local wifi = wifi
local net = net
local WIFI_CONSTANT = require("ihome.constant").WIFI
local SSID = WIFI_CONSTANT["SSID"]
local PWD = WIFI_CONSTANT["PWD"]
--==================================
function wifista:__reg_def()
    wifi.eventmon.register(
        wifi.eventmon.STA_GOT_IP,
        function()
            print("wifi con suc!")
            self.onfunc_()
        end
    )
    wifi.eventmon.register(
        wifi.eventmon.STA_DISCONNECTED, 
        function()
            print("wifi connect failed!")
            self.failfunc_()
        end)
end

function wifista:__conenct()
    print("wifista set ap...")
    wifi.setmode(wifi.STATION)
    --配置AP名称和密码
    local stacfg = {}
    stacfg.ssid = SSID
    stacfg.pwd = PWD
    wifi.sta.config(stacfg)
    wifi.sta.autoconnect(0)
    print("wait con...")
end

function wifista:on(onfunc, failfunc)
    if type(onfunc) == "function" then 
        self.onfunc_ = onfunc
    end

    self:__reg_def()
    self:__conenct()
    return self
end

function wifista:fail(failfunc)
    if type(failfunc) == "function" then 
        self.failfunc_ = failfunc
    end
    return self
end

return wifista
